/***************************************************************************
 *   Copyright (C) 2007 by Phillip Aldridge   *
 *   phillip@imi21.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "icontemp.h"
#include "config.h"
#include <qfont.h>
#include <qcolor.h>
#include <qpen.h>
#include <qdatetime.h>

/**
   This draws all the temperature icon
   TODO nicer drawing 

*/

IconTemp::IconTemp(QWidget *parent, const char *name)
 : QWidget(parent, "iconTemp")
{
   qDebug("Setting up iconTemp");
   width=100; height=100;
   setFixedSize (width,height);
   maxPos=_TEMP_MAX;
   minNeg=_TEMP_MIN;
   range=maxPos - minNeg;
   scale = height/range;
}

IconTemp::~IconTemp()
{
}

void IconTemp::setStationData(StationData *s)
{
   stationdata=s;
}

void IconTemp::save()
{
   const QString file=QString("%1/%2").arg(_FTP_LOCAL_FILES).arg(_FTP_TEMPERATURE);
   QPixmap pm( width, height );
   bitBlt( &pm,0,0, this, 0, 0, -1,-1 );
   pm.save(file,"PNG",80);
}

void IconTemp::paintEvent( QPaintEvent *e )
{ 
   //QPixmap pm( width, height );
   QPainter painter(this);
  // painter.begin( &pm);
   QPen pen = painter.pen();
  
  // pm.fill( this, 0, 0);//  fill background
   painter.fillRect(0, 0, width, height, Qt::white);

   pen.setStyle ( SolidLine );
   QFont ft( "Helvetica", 8, QFont::Bold );
    painter.setFont( ft );
    painter.setPen( Qt::black );

    painter.fillRect (40,0,20,60,Qt::red );
    painter.fillRect (40,61,20,100,Qt::blue );
/**

   Make a verticle scale based on max & min temperature
*/

int zeroPos =  vPos(0);
int tmax= vPos( stationdata->tempMax() );
int tmin= vPos( stationdata->tempMin() );
int tnow= vPos( stationdata->tempOut() );
int trange =(int) ( stationdata->tempMax() - stationdata->tempMin() );

   painter.fillRect (45,tmax,10, trange, Qt::white );
   
   painter.setPen( Qt::blue );
   painter.drawLine(35,tmin,65,tmin);
   
   painter.setPen( Qt::red );
   painter.drawLine(35,tmax,65,tmax);
   
   painter.setPen( Qt::green );
   painter.drawLine(30,tnow,70,tnow);

   painter.setPen( Qt::black );
   painter.drawLine(40,zeroPos,60,zeroPos);
   painter.drawText( 4, 10,tr("Temperature %1").arg(_UNIT_TEMP) );
   painter.drawText(75,tnow+4,QString("%1").arg(stationdata->tempOut()) );
   painter.setPen( Qt::gray );
   painter.drawText(7,tmax-6,QString("%1").arg(stationdata->tempMax()));
   painter.drawText(7,tmin+6,QString("%1").arg(stationdata->tempMin()));

   save();
}

int IconTemp::vPos(float p=0)
{
   return (int) (height - (  (p +maxPos) * scale ));
}
